<?php

namespace Api\Controller;

use Common\Utils\YouaErp;
use Think\Controller;

header('Content-type: text/html; charset=utf-8');
header('Access-Control-Allow-Origin:*'); //允许跨域

class DataTransferController extends ApiCommonController
{
    protected $erpkey = "ERP8888";
    protected $YouaErp;
    protected $Model;

    public function __construct()
    {
        // parent::__construct();
        $this->YouaErp = new YouaErp();
        $this->Model   = M(null, null, 'DB_YMALL_CMS');

    }

    public function Receive_Data($table_name, $data, $url)
    {
        $time = time();
        if (strpos("ymall_item|ymall_stock", $table_name) !== false) {
//data数据要重组
            $new_data  = array();
            $table_key = '';
            $i         = 0;
            foreach ($data as $key => $val) {
                foreach ($data[$key] as $key2 => $val2) {
                    $table_key = $key2;
                }
                // echo "table_key=".$table_key."-----".$val[$table_key]."<br>";
                $arr_key                   = explode('|', $table_key);
                $arr_value                 = explode('|', $val[$table_key]);
                $new_data[$i][$arr_key[0]] = $arr_value[0];
                $new_data[$i][$arr_key[1]] = $arr_value[1];
                $new_data[$i][$arr_key[2]] = $arr_value[2];
                $i++;
            }
            $data = array();
            $data = $new_data;
        }
        $arr_sql = array();
        $params  = array(
            "partner"   => "YAYS0001",
            "TimeStamp" => $time,
            "data"      => $data,
        );
        $return_data = $this->YouaErp->return_data($params, "YAYS8888", $url);
        $arr_data    = json_decode($return_data, true);
        //数组资料处理
        if ($arr_data['returnCode'] == 1) {
            $receive_data = $arr_data['data'];
            # var_dump($receive_data);
            $arr_sql = $this->YouaErp->return_sql($receive_data, $table_name, $this->Model);
        }
        return $arr_sql;
    }

    public function index()
    {
        try {
            $stime = microtime(true);
            //抓取所有有异动的临时档资料
            $temp_data = $this->Model->table("temp_data")->where("is_transfer = ''")->field("table_name,key,value")->select();

            ######start组织架构erp=>mysql#######
            $arr_sql1  = array();
            $arr_temp1 = array();
            $data      = $this->YouaErp->new_arr($temp_data, "ymall_erp_group");
            $url       = "http://218.76.51.168:8088/V0/GetDept.ashx?";
            if (count($data) > 0) {
                $arr_sql1 = $this->Receive_Data("ymall_erp_group", $data, $url);
                if (count($arr_sql1) > 0) //有抓到条件的资料，如果条件并未抓取到资料不更新Y，然后检查临时档。
                {
                    $arr_temp1 = $this->YouaErp->Update_Temp_Data($data, "ymall_erp_group");
                }

                $arr_sql1 = array_merge($arr_sql1, $arr_temp1);
                error_log(date("Y-m-d H:i:s") . "组织架构SQL:" . json_encode($arr_sql1, JSON_UNESCAPED_UNICODE) . "\r\n", 3, C('LOG_PATH') . date('y_m_d') . '_trans.log');

            }
            ######end组织架构erp=>mysql#######

            //////start人员信息erp=>mysql//////
            $arr_sql2  = array();
            $arr_temp2 = array();
            $data      = $this->YouaErp->new_arr($temp_data, "sys_user");
            $url       = "http://218.76.51.168:8088/V0/GetPerson.ashx?";
            if (count($data) > 0) {
                $arr_sql2 = $this->Receive_Data("sys_user", $data, $url);
                if (count($arr_sql2) > 0) //有抓到条件的资料
                {
                    $arr_temp2 = $this->YouaErp->Update_Temp_Data($data, "sys_user");
                }

                $arr_sql2 = array_merge($arr_sql2, $arr_temp2);
                error_log(date("Y-m-d H:i:s") . "人员信息SQL:" . json_encode($arr_sql2, JSON_UNESCAPED_UNICODE) . "\r\n", 3, C('LOG_PATH') . date('y_m_d') . '_trans.log');

            }

            //////end人员信息erp=>mysql//////

            ######start分类信息erp=>mysql######
            $arr_sql3  = array();
            $arr_temp3 = array();
            $data      = $this->YouaErp->new_arr($temp_data, "ymall_category");
            $url       = "http://218.76.51.168:8088/V0/GetCategory.ashx?";
            if (count($data) > 0) {
                $arr_sql3 = $this->Receive_Data("ymall_category", $data, $url);
                if (count($arr_sql3) > 0) {
                    $arr_temp3 = $this->YouaErp->Update_Temp_Data($data, "ymall_category");
                }
                $arr_sql3 = array_merge($arr_sql3, $arr_temp3);
                error_log(date("Y-m-d H:i:s") . "分类信息SQL:" . json_encode($arr_sql3, JSON_UNESCAPED_UNICODE) . "\r\n", 3, C('LOG_PATH') . date('y_m_d') . '_trans.log');

            }
            ######end分类信息erp=>mysql######

            //////start商品信息erp=>mysql//////10
            $arr_sql4  = array();
            $arr_temp4 = array();
            $data      = $this->YouaErp->new_arr($temp_data, "ymall_item");
            $url       = "http://218.76.51.168:8088/V0/GetGoods.ashx?";
            if (count($data) > 0) {
                $arr_sql4 = $this->Receive_Data("ymall_item", $data, $url);
                if (count($arr_sql4) > 0) {
                    $arr_temp4 = $this->YouaErp->Update_Temp_Data($data, "ymall_item");
                }

                $arr_sql4 = array_merge($arr_sql4, $arr_temp4);
                //将临时档的资料是否转档赋值为Y
                error_log(date("Y-m-d H:i:s") . "商品信息SQL:" . json_encode($arr_sql4, JSON_UNESCAPED_UNICODE) . "\r\n", 3, C('LOG_PATH') . date('y_m_d') . '_trans.log');

            }
            #var_dump($arr_sql4);exit;

            //////end商品信息erp=>mysql//////10
            #######start获取品牌信息######
            $data      = $this->YouaErp->new_arr($temp_data, "ymall_brand");
            $url       = "http://218.76.51.168:8088/V0/GetBrannd.ashx?";
            $arr_sql5  = array();
            $arr_temp5 = array();
            if (count($data) > 0) {
                $arr_sql5 = $this->Receive_Data("ymall_brand", $data, $url);
                if (count($arr_sql5) > 0) {
                    $arr_temp5 = $this->YouaErp->Update_Temp_Data($data, "ymall_brand");
                }
                $arr_sql5 = array_merge($arr_sql5, $arr_temp5);
                //将临时档的资料是否转档赋值为Y
                error_log(date("Y-m-d H:i:s") . "品牌信息SQL:" . json_encode($arr_sql5, JSON_UNESCAPED_UNICODE) . "\r\n", 3, C('LOG_PATH') . date('y_m_d') . '_trans.log');

            }

            #######end获取品牌信息######
            $all_sql = array_merge($arr_sql1, $arr_sql2, $arr_sql3, $arr_sql4, $arr_sql5);
            if (count($all_sql) > 0) {
                if ($this->YouaErp->trans($all_sql, $this->Model)) {
                    echo "erp(oracle)->mysql资料转档成功！";
                }
            } else {
                echo "无需要转档的资料，请稍候再试！";
            }
            $etime = microtime(true); #获取程序执行结束的时间
            $total = $etime - $stime; #计算差值
            echo "<br />运行时间={$total} times";
        } catch (\Exception $e) {
            $msg = "资料转档失败,数据异常或接口返回数据time out";
            error_log(date("Y-m-d H:i:s") . $msg . "\r\n", 3, C('LOG_PATH') . date('y_m_d') . '_trans.log');
            echo $e->getMessage();

        }

    }

    /*取得组织代号异动资料*/
    public function GetDept_Notice()
    {
        //存temp_data
        $this->Post_Data("ymall_erp_group");
    }

    /*取得人员信息异动通知资料*/
    public function GetPerson_Notice()
    {

        $this->Post_Data("sys_user");
    }

    /*取得分类信息异动通知资料*/
    public function GetCategory_Notice()
    {
        $this->Post_Data("ymall_category");
    }

    /*取得商品信息异动通知资料*/
    public function GetGoods_Notice()
    {
        $this->Post_Data("ymall_item");
    }

    /*取得品牌异动通知资料*/
    public function GetBrannd_Notice()
    {
        $this->Post_Data("ymall_brand");
    }

    /*取得库存异动通知资料*/
    public function GetStock_Notice()
    {
        $this->Post_Data("ymall_stock");
    }

    public function Post_Data($table_name)
    {
        $data = json_decode($_POST['request'], true);
        $sign = $data['sign'];
        unset($data['sign']);
        $sign2 = $this->YouaErp->makeSign($data, $this->erpkey);
        if ($sign != $sign2) {
            $this->YouaErp->toJson('签名错误!', '', -1);
        }
        $arr_sql = $this->YouaErp->temp_sql($data['data'], $table_name);
        if ($this->YouaErp->trans($arr_sql, $this->Model)) {
            $this->YouaErp->toJson('成功', '', 1);

        }
    }

    public function Data_Udate_Notice()
    {
        $data       = json_decode($_POST['request'], true);
        $sign       = $data['sign'];
        $table_name = $data['table_name'];
        unset($data['sign']);
        $sign2 = $this->YouaErp->makeSign($data, $this->erpkey);
        if ($sign != $sign2) {
            $this->YouaErp->toJson('签名错误!', '', -1);
        }
        $arr_sql = $this->YouaErp->temp_sql($data['data'], $table_name);
        if ($this->YouaErp->trans($arr_sql, $this->Model)) {
            $this->YouaErp->toJson('成功', '', 1);

        }
    }

    public function update_stock()
    {
        header('Content-type: text/html; charset=utf-8');
        $data_key = "YAYS8888";
        $time     = time();
        $url      = "http://218.76.51.168:8088/V0/GetStock.ashx?";
        $Model    = M(null, null, 'DB_YMALL_CMS');
        ######start更新库存erp=>mysql#######
        $data = array(array('sku' => "KECMY2819-DRD11", 'sellerPath' => '26130001'),
            array('sku' => "KECMY2819-DJT19", 'sellerPath' => '26130001'),
            array('sku' => "2110000458000", 'sellerPath' => '26130001'));
        $params = array(
            "partner"   => "YAYS0001",
            "TimeStamp" => $time,
            "data"      => $data,
        );
        $return_data = $this->YouaErp->return_data($params, $data_key, $url);
        $return_data = json_decode($return_data, true);
        var_dump($return_data);
        ######end更新库存erp=>mysql#######

    }

    /* public function index2() //测试的。
{
header('Content-type: text/html; charset=utf-8');
$time = time();
#$time = "1536989387";
$data[]["sellerPath"] = "68120002";
$data[]["sellerPath"] = "68120001";
$data2 = json_encode($data);
$params = array(
"partner" => "YAYS0001",
"TimeStamp" => $time,
"data" => $data2,
);

foreach ($params as $key => $value) {
$arr[strtolower($key)] = $key . "=" . $value;

}
ksort($arr);
//排序后，把key换回来
$params = array();
foreach ($arr as $key => $value) {
$new_arr = explode("=", $value);
$params[$new_arr[0]] = $new_arr[1];
}
#重新拼接参数1=值1&参数2=值2...用http_build_query()函数有乱码
foreach ($params as $key => $value) {
$paramstring = isset($paramstring) ? $paramstring . "&" . $key . "=" . $value : $key . "=" . $value;
}
$key = "YAYS8888";
echo "<br>paramstring" . $paramstring;
$sign = md5($paramstring . $key);
echo "<br>sign=" . $sign;
$params2 = array(
"partner" => "YAYS0001",
"sign" => $sign,
"TimeStamp" => $time,
"data" => $data

);
$request = "request=" . json_encode($params2);
echo "<br><br>";
var_dump($request);
$url = "http://218.76.51.168";
$url = $url . ":8088/V0/GetDept.ashx?";
$http_url = "http://www.baidu.com";
$return_data = get_curl($url, $request);
$return_data = json_decode($return_data, true);
var_dump($return_data);

}*/

}
